package top.college.screen;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JPasswordField;
import javax.swing.JTextField;

import top.college.dao.JDBC_ConnClose;
import top.college.dao.JDBC_SQL;

public class LoginMain {
	public static String name;
	
	public static String loginScreen(JTextField IDinsert, JPasswordField PWinsert) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		name = null;
		try {
			conn = JDBC_ConnClose.openDataBase();
			pstmt = conn.prepareStatement( JDBC_SQL.LoginAdminSelect );
			pstmt.setString( 1, IDinsert.getText() );
			pstmt.setString( 2, PWinsert.getText() );
			rs = pstmt.executeQuery();
			if ( rs.next() ) {
				name = rs.getString(1);
				return "admin";
			} else {
				pstmt.close();
				pstmt = conn.prepareStatement( JDBC_SQL.LoginProfSelect );
				pstmt.setString( 1, IDinsert.getText() );
				pstmt.setString( 2, PWinsert.getText() );
				rs = pstmt.executeQuery();
				if ( rs.next () ) {
					name = rs.getString(1);
					return "prof";
				} else {
					pstmt.close();
					pstmt = conn.prepareStatement( JDBC_SQL.LoginStdSelect );
					pstmt.setString( 1, IDinsert.getText() );
					pstmt.setString( 2, PWinsert.getText() );
					rs = pstmt.executeQuery();
					if ( rs.next () ) {
						name = rs.getString(1);
						return "std";
					} 
				}
			}
		} catch ( Exception e ) {
			e.printStackTrace();
		} finally {
			JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
		}
		return "null";
	}
}